package cn.com.truelore;

import java.util.LinkedHashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.view.WindowManager;
import cn.com.truelore.base.BaseActivity;
import cn.com.truelore.common.Constants;

import com.google.android.c2dm.C2DMessaging;

/**
 * Sell smart main activity android
 * 
 * @author murui
 * 
 */
public class MainActivity extends BaseActivity {

	public static String SENDER_ID = ""; // 使用C2DM服务的用户账户
	public static String MESSAGE_KEY_ONE = ""; // 和服务器商量好的接收消息的键值key

	AlertDialog _alertDialog = null;
	private String _applicationFlg = "";

	public MainActivity() {
		super(R.layout.main);
	}

	public void initPageData() {
		super.initPageData();
		SENDER_ID = this.getResources().getString(R.string.c2dmsenderid);
		MESSAGE_KEY_ONE = this.getResources().getString(R.string.c2dmmessagekey);
		_applicationFlg = super.getSharedPreferences(Constants.SP_GLOBALMEMORY,
				Constants.SP_GLOBALMEMORY_FIRST);
		if (Constants.SP_GLOBALMEMORY_FIRST_1.equals(_applicationFlg) == false) {
			String strTitle = getResources().getString(
					R.string.customWarningTitle);
			String strContext = getResources().getString(
					R.string.customWarningInfo);
			AlertDialog.Builder ad = new AlertDialog.Builder(MainActivity.this);
			ad.setTitle(strTitle);
			ad.setMessage(strContext);
			ad.setPositiveButton("同意",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int i) {
							initPageData1();
							initGlobalMemoryData();
						}
					});
			ad.setNegativeButton("不同意",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int i) {
							_alertDialog.dismiss();
							MainActivity.this.finish();
						}
					});
			_alertDialog = ad.create();
			_alertDialog.show();
		} else {
			initPageData1();
		}

	}

	private void initPageData1() {
		getWindow().setFormat(PixelFormat.RGBA_8888);
		// getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
		new Handler().postDelayed(new Runnable() {
			public void run() {
				/*
				 * Create an Intent that will start the Main WordPress Activity.
				 */
				Intent mainIntent = new Intent(MainActivity.this,
						HomeActivity.class);
				mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				MainActivity.this.startActivity(mainIntent);
				MainActivity.this.finish();
			}
		}, 500); // 2900 for release
	}

	/**
	 * 初始化全局变量
	 */
	public void initGlobalMemoryData() {

		C2DMessaging.register(this, SENDER_ID);
		// set first flg
		super.setSharedPreferences(Constants.SP_GLOBALMEMORY,
				Constants.SP_GLOBALMEMORY_FIRST,
				Constants.SP_GLOBALMEMORY_FIRST_1);
		Map<String, String> map = new LinkedHashMap<String, String>();
		map.put("1", "Corn");
		map.put("2", "Corn");
		map.put("3", "HRW");
		map.put("4", "HRW");
		map.put("5", "SRW");
		map.put("6", "SRW");
		map.put("7", "SWW");
		map.put("8", "SWW");
		map.put("9", "HRS");
		map.put("10", "HRS");
		map.put("11", "Soybeans");
		map.put("12", "Soybeans");
		super.setSharedPreferences(Constants.SP_COMMDNAMES, map);
		map = new LinkedHashMap<String, String>();
		map.put(String.valueOf(Constants.TYPE_CORN), "Corn");
		map.put(String.valueOf(Constants.TYPE_SOY), "Soy");
		map.put(String.valueOf(Constants.TYPE_WHEAT), "Wheat");
		map.put(String.valueOf(Constants.TYPE_SETTINGS), "Settings");
		super.setSharedPreferences(Constants.SP_TAB_NAMES, map);
		// Old new type
		map = new LinkedHashMap<String, String>();
		map.put(String.valueOf(Constants.TYPE_OLD), "Old");
		map.put(String.valueOf(Constants.TYPE_NEW), "New");
		super.setSharedPreferences(Constants.SP_TYPE_NEW_OLD, map);
	}
}